package secondkill.rabbtmq;

import cn.hutool.json.JSONUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import secondkill.pojo.SecKillMessage;
import secondkill.pojo.TSeckillOrder;
import secondkill.pojo.TUser;
import secondkill.service.ITGoodsService;
import secondkill.service.ITOrderService;
import secondkill.vo.GoodsVo;

/**
 * @Package: com.seckillpro.seckill_pro.rabbtmq
 * @ClassName: MQReceiver
 * @Author: lyx
 * @CreateTime: 2021/3/26 17:18
 * @Description:
 */

@Slf4j
@Service
public class MQReceiver {

/*    //Fanout模式
    @RabbitListener(queues = "Queue_Fanout01")
    public void receive_Fanout01(Object msg) {
        log.info("Queue_Fanout01接收消息 ：" + msg);
    }
    @RabbitListener(queues = "Queue_Fanout02")
    public void receive_Fanout02(Object msg) {
        log.info("Queue_Fanout02接收消息 ：" + msg);
    }

    //Direct模式
    @RabbitListener(queues = "Queue_Direct01")
    public void receive_Direct01(Object msg) {
        log.info("Queue_Direct01接收消息 ：" + msg);
    }
    @RabbitListener(queues = "Queue_Direct02")
    public void receive_Direct02(Object msg) {
        log.info("Queue_Direct02接收消息 ：" + msg);
    }
    //Topic模式
    @RabbitListener(queues = "Queue_Topic01")
    public void receive_Topic01(Object msg) {
        log.info("Queue_Topic01接收消息 ：" + msg);
    }
    @RabbitListener(queues = "Queue_Topic02")
    public void receive_Topic02(Object msg) {
        log.info("Queue_Topic02接收消息 ：" + msg);
    }
    //Headers模式
    @RabbitListener(queues = "Queue_Headers01")
    public void receive_Headers01(Message msg) {
        log.info("Queue_Headers接收Message消息对象 ：" + msg);
        log.info("Queue_Headers接收消息" + new String(msg.getBody()));
    }
    @RabbitListener(queues = "Queue_Headers02")
    public void receive_Headers02(Message msg) {
        log.info("Queue_Headers02接收消息 ：" + msg);
        log.info("Queue_Headers接收消息" + new String(msg.getBody()));
    }*/

    @Autowired
    private ITGoodsService goodsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITOrderService orderService;

    /**
     * 下单操作
     * @param message
     */
    @RabbitListener(queues = "seckillQueue")//监听队列
    public void receive(String message) {

        log.info("message:" + message);
        SecKillMessage secKillMessage = JSONUtil.toBean(message, SecKillMessage.class);

        Long goodsId = secKillMessage.getGoodId();
        TUser user = secKillMessage.getUser();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodId(goodsId);

        //判断库存
        if (goodsVo.getStockCount() < 1) {
            return;
        }

        //判断是否重复抢购
        TSeckillOrder secKillOrder = (TSeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);

        if (secKillOrder != null) {
            return;
        }

        //下单操作
        orderService.secKill(user, goodsVo);
    }

}
